package com.intretech.umsin.gateway.config.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 放行白名单配置
 *
 */
@Configuration
@RefreshScope
@ConfigurationProperties(prefix = "security.ignore")
public class IgnoreWhiteProperties {

    /**
     * 监控中心和swagger需要访问的url
     */
    private static final String[] ENDPOINTS = {
            "/**/actuator/**", "/**/actuator/**/**",  //端点监控
            "/**/doc.html", "/doc.html",  //端点监控
            "/v2/api-docs/**", "/swagger-ui.html", "/swagger-resources/**", "/webjars/**",            // api-gateway webflux swagger
            "/**/v2/api-docs/**", "/**/swagger-ui.html", "/**/swagger-resources/**", "/**/webjars/**", //业务中心swagger
            "/**/turbine.stream", "/**/turbine.stream**/**", "/**/hystrix", "/**/hystrix.stream", "/**/hystrix/**", "/**/hystrix/**/**", "/**/proxy.stream/**", //熔断监控
            "/**/druid/**", "/**/favicon.ico", "/**/prometheus"
    };

    /**
     * 放行白名单配置，网关不校验此处的白名单
     */
    private String[] whites;

    public String[] getWhites() {
        if (whites == null || whites.length == 0) {
            return ENDPOINTS;
        }

        List<String> list = new ArrayList<>();
        for (String url : ENDPOINTS) {
            list.add(url);
        }
        for (String url : whites) {
            list.add(url);
        }

        return list.toArray(new String[list.size()]);
    }

    public void setWhites(String[] whites) {
        this.whites = whites;
    }

    public IgnoreWhiteProperties() {
    }

    @Override
    public String toString() {
        return "IgnoreWhiteProperties{" +
                "whites=" + Arrays.toString(whites) +
                '}';
    }
}
